﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;

namespace CTEA_INDIA
{
    public partial class Hire_Equipments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["CTEA"].ConnectionString;
            rdDeliveredToCustomer.Checked = true;
            txtStrtCal.StartDate = DateTime.Today;
            
            //cmbSearchHireEquip.Items.RemoveAt(3);
            BAL.Customers.PopulateLoc(ref cmbSrchLoc);
            if (Session["UserID"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            int i;

            if (!IsPostBack)
            {
                populateHireEqupm();
                BAL.Customers.PopulateLoc(ref cmbLocation);
                

            }

            for (i = 1; i <= 12; i++)
            {
                cmbDelHr.Items.Add(i.ToString());
                cmbRecHr.Items.Add(i.ToString());

                cmbEdtDelHr.Items.Add(i.ToString());
                cmbEdtRecHr.Items.Add(i.ToString());
            }

            cmbDelMin.Items.Add("0");
            cmbRecMin.Items.Add("0");


            cmbEdtDelMin.Items.Add("0");
            cmbEdtRecMin.Items.Add("0");

            for (i = 1; i <= 59; i++)
            {
                cmbDelMin.Items.Add(i.ToString());
                cmbRecMin.Items.Add(i.ToString());

                cmbEdtDelMin.Items.Add(i.ToString());
                cmbEdtRecMin.Items.Add(i.ToString());
            }
            
        }

        protected void IsNotInvalid(object sender, ServerValidateEventArgs args )
        {

            DateTime str , end ;
            bool strDate = DateTime.TryParse(txtStartDate.Text, out str);

            bool IsDate = DateTime.TryParse(args.Value.ToString(), out end);

            if (IsDate && strDate)
            {
                if (end.Date < str.Date)
                    args.IsValid = true;
                else
                    args.IsValid = false;

            }
 
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string Company_Name = txtCompanyName.Text;
            string Authority_Name = txtAuthorityName.Text;
            string _Address = txtAddress.Text;
            string Title_Of_Production = cmbTitleOfProduction.Text;
            string Language_Of_Production = cmbLanguageOfProduction.Text;
            
            string mot;
            if (rdReceivedFromShop.Checked.Equals(true))
            {
                 mot = "ReceivedFromShop";
            }
            else
            {
                 mot = "DeliveredToCustomer";
            }
            string Equipment_Location = cmbLocation.Text;
            string Period_Of_Production = cmbPeriodOfProduction.Text;
            string Start_Date = txtStartDate.Text;
            string End_Date = txtEndDate.Text;
            
            DateTime startTime = DateTime.Parse(txtStartDate.Text);
            DateTime endTime = DateTime.Parse(txtEndDate.Text);
            TimeSpan span = endTime.Subtract ( startTime );

            string Total_Days = span.Days.ToString();
            
            string delHr = cmbDelHr.Text;
            string delMin = cmbDelMin.Text;
            string delAmPm = cmbDelAmPm.Text;

            string recHr = cmbRecHr.Text;
            string recMin = cmbRecMin.Text;
            string recAmPm = cmbRecAmPm.Text;

            string _Hire_Equipment = cmbHireEquipment.Text;
            string Payment_Mode = cmbPaymentMode.Text;


            string _Cash , Cheque_No, Amount_Cheque ;

            if (Payment_Mode == "Cash")
            {

                _Cash = txtCash.Text;
                 Cheque_No = null;
                 Amount_Cheque = null;


            }
            else
            {

                 _Cash = null;
                 Cheque_No = txtChequeNo.Text;
                 Amount_Cheque = txtAmount.Text;
            }

            string _Balance = txtBalance.Text;

            

                BAL.Customers.Hire_Equipments(Company_Name, Authority_Name, _Address, Title_Of_Production, Language_Of_Production, mot, Equipment_Location, Period_Of_Production, Start_Date, End_Date, Total_Days,
                    delHr, delMin, delAmPm, recHr, recMin, recAmPm, _Hire_Equipment, Payment_Mode, _Cash, Cheque_No, Amount_Cheque, _Balance );
           
                spnMsg.Visible = true;
                spnText.InnerHtml = "Your details have been submitted.";


                txtCompanyName.Text = "";
                txtAuthorityName.Text = "";
                txtAddress.Text = "";
                cmbTitleOfProduction.Text = "";
                cmbLanguageOfProduction.Text = "";

                
                rdReceivedFromShop.Checked = true ;
                cmbLocation.SelectedIndex = 0;
                cmbPeriodOfProduction.SelectedIndex = 0; 
                txtStartDate.Text = "";
                txtEndDate.Text = "";
                txtTotalNoDays.Text = "";


                cmbDelHr.SelectedIndex = 0;
                cmbDelMin.SelectedIndex = 0;
                cmbDelAmPm.SelectedIndex = 0;

                cmbRecHr.SelectedIndex = 0;
                cmbRecMin.SelectedIndex = 0; 
                cmbRecAmPm.SelectedIndex = 0; 

                cmbHireEquipment.SelectedIndex = 0;
                cmbPaymentMode.SelectedIndex = 0; 


                txtCash.Text = "";
                txtChequeNo.Text = "";
                txtAmount.Text = "";
                txtBalance.Text = "";

                populateHireEqupm();

            
        }


        public void populateHireEqupm()
        {

            DataSet ds = BAL.Customers.ViewllEquipments();
            gdHireEquipment.DataSource = ds;
            gdHireEquipment.DataBind();

        }

        protected void gdHireEquipment_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdHireEquipment.PageIndex = e.NewPageIndex;
            populateHireEqupm();
        }

        protected void gdHireEquipment_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lEdit = (LinkButton)e.Row.FindControl("lnkEdit");
                Button update = (Button)e.Row.FindControl("btnUpdate");
                LinkButton lDelete = (LinkButton)e.Row.FindControl("lnkDelete");
                lDelete.CommandArgument = e.Row.RowIndex.ToString();
                LinkButton lnkApp = (LinkButton)e.Row.FindControl("lnkApprove");
                


                string strapp = lnkApp.ToolTip;

               
                    if (strapp.Equals("1"))
                    {
                        lnkApp.Text = "Disapprove";
                    }
                    else { lnkApp.Text = "Approve"; }
            

            }

        }

        protected void lnkView_Click(object sender, EventArgs e)
        {
            LinkButton lnkVw = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkVw.Parent.Parent;
            

            int Cid = Convert.ToInt32(gdHireEquipment.DataKeys[row.RowIndex]["HireId"]);


            string[] Details = BAL.Customers.GetHireEquipmentById(Cid);

            lblCompanyName.Text = Details[1];
            lblAuthorityName.Text = Details[2];
            lblAddress.Text = Details[3];
            lblTitleOfProduction.Text = Details[4];
            lblLanguageOfProduction.Text = Details[5];
            lblmot.Text = Details[6];
            lblLocation.Text = Details[7];
            lblPop.Text = Details[8];
            lblStrDate.Text = DateTime.Parse(Details[9].ToString()).ToShortDateString().ToString();
            lblEndDate.Text = DateTime.Parse(Details[10].ToString()).ToShortDateString().ToString();
            lblTotalDays.Text = Details[11];
            lblDelTime.Text = Details[12] + ":" + Details[13] + ":" + Details[14];
            lblRecTime.Text = Details[15] + ":" + Details[16] + ":" + Details[17];
            lblHoe.Text = Details[18];
            lblPayMode.Text = Details[19];
            lblBalance.Text = Details[23];



            lnkView_ModalPopupExtender.Show();

            //document.getElementById("totalDaystd").innerHTML = "<asp:TextBox ID=\"txtDaysText\" runat=\"server\" ReadOnly=\"True\" Text=\""+ days +"\"></asp:TextBox>";

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string columnName = cmbSearchHireEquip.SelectedValue, Value = "";

            
            if (cmbSearchHireEquip.SelectedValue.Equals("0"))
            {
                populateHireEqupm();
            }
            else if (cmbSearchHireEquip.SelectedValue.Equals("1"))
            {
                //columnName = "Company_Name";
                Value = txtSrchName.Text;
                DataSet ds =  BAL.Customers.SearchHireEquipment(columnName, Value);
                gdHireEquipment.DataSource = ds;
                gdHireEquipment.DataBind();
            }
            else if (cmbSearchHireEquip.SelectedValue.Equals("2"))
            {
                //columnName = "Title_Of_Production";
                Value = cmbSrchTOP.Text;

                DataSet ds = BAL.Customers.SearchHireEquipment(columnName, Value);
                gdHireEquipment.DataSource = ds;
                gdHireEquipment.DataBind();
            }
            else if (cmbSearchHireEquip.SelectedValue.Equals("3"))
            {
                //columnName = "Equipment_Location";
                Value = cmbSrchLoc.Text;

                DataSet ds = BAL.Customers.SearchHireEquipment(columnName, Value);
                gdHireEquipment.DataSource = ds;
                gdHireEquipment.DataBind();
            }

            

        }

        protected void lnkEdit_Click(object sender , EventArgs e)
        {
            LinkButton lnkEdit = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkEdit.Parent.Parent;

            int Cid = Convert.ToInt32(gdHireEquipment.DataKeys[row.RowIndex]["HireId"]);
            string[] Details = BAL.Customers.GetHireEquipmentById(Cid);
            ViewState["HireId"] = Details[0];


            txtEdtComName.Text = Details[1];
            txtEdtAuthorityName.Text = Details[2];
            txtEdtAddr.Text = Details[3];
             


            cmbEdtTOP.Text = Details[4] ;
            cmbEdtLOP.Text = Details[5];

            string mot = Details[6];
            if (mot.Equals("ReceivedFromShop") )
            {
               rdEdtRFS.Checked= true ;
            }
            else
            {
                rdEdtDTC.Checked = true ;
            }

            cmbEdtLocation.SelectedValue = Details[7] ;
            cmbEdtPOP.SelectedValue = Details[8];
            txtEdtStartDate.Text =DateTime.Parse(Details[9]).ToShortDateString();
            txtEdtEndDate.Text = DateTime.Parse(Details[10]).ToShortDateString();

            txtEdtTotalDays.Text =  Details[11] ;


            cmbEdtDelHr.SelectedValue = Details[12];
            cmbEdtDelMin.SelectedValue = Details[13];
            cmbEdtDelAmPm.SelectedValue =  Details[14];

            cmbEdtRecHr.SelectedValue = Details[15];
            cmbEdtRecMin.SelectedValue =  Details[16];
            cmbEdtRecAmPm.SelectedValue = Details[17] ;

            cmbEdtHOE.SelectedValue = Details[18];
            cmbEdtPaymentMode.SelectedValue = Details[19];


            
       
            txtEdtCash.Text = Details[20];

            if (Details[19].Equals("Cash"))
            {
                ChequeEdt.Style.Add("Visibility", "collapse");
                ChequeEdt2.Style.Add("Visibility", "collapse");
                cashEdt.Style.Add("Visibility", "Visible");
            }
            else if (Details[19].Equals("Cheque"))
            {
                ChequeEdt.Style.Add("Visibility", "Visible");
                ChequeEdt2.Style.Add("Visibility", "Visible");
                cashEdt.Style.Add("Visibility", "collapse");
            }

            
            txtEdtChequeNo.Text = Details[21];
            txtEdtAmount.Text = Details[22];
            

            txtEdtBal.Text = Details[23];
            cmbEdtLocation.SelectedValue = Details[24];


            LnkEditPopUp.Show();

        }

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            LinkButton lnkEdit = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkEdit.Parent.Parent;

            int Cid = Convert.ToInt32(gdHireEquipment.DataKeys[row.RowIndex]["HireId"]);
            BAL.Customers.DeleteHireEquipById(Cid);
            populateHireEqupm();
        }

        protected void update(object sender, EventArgs e)
        {
            string Company_Name = txtEdtComName.Text;
            string Authority_Name = txtEdtAuthorityName.Text;
            string _Address = txtEdtAddr.Text;
            string Title_Of_Production = cmbEdtTOP.Text;
            string Language_Of_Production = cmbEdtLOP.Text;

            string mot;
            if (rdEdtRFS.Checked.Equals(true))
            {
                mot = "ReceivedFromShop";
            }
            else
            {
                mot = "DeliveredToCustomer";
            }
            string Equipment_Location = cmbEdtLocation.Text;
            string Period_Of_Production = cmbEdtPOP.Text;
            string Start_Date = txtEdtStartDate.Text;
            string End_Date = txtEdtEndDate.Text;

            DateTime startTime = DateTime.Parse(txtEdtStartDate.Text);
            DateTime endTime = DateTime.Parse(txtEdtEndDate.Text);
            TimeSpan span = endTime.Subtract(startTime);

            string Total_Days = span.Days.ToString();


            string delHr = cmbEdtDelHr.Text;
            string delMin = cmbEdtDelMin.Text;
            string delAmPm = cmbEdtDelAmPm.Text;

            string recHr = cmbEdtRecHr.Text;
            string recMin = cmbEdtRecMin.Text;
            string recAmPm = cmbEdtRecAmPm.Text;

            string _Hire_Equipment = cmbEdtHOE.Text;
            string Payment_Mode = cmbEdtPaymentMode.Text;


            string _Cash, Cheque_No, Amount_Cheque;

            if (Payment_Mode == "Cash")
            {

                _Cash = txtEdtCash.Text;
                Cheque_No = null;
                Amount_Cheque = null;


            }
            else
            {

                _Cash = null;
                Cheque_No = txtEdtChequeNo.Text;
                Amount_Cheque = txtEdtAmount.Text;
            }

            string _Balance = txtEdtBal.Text;

            string HireId = ViewState["HireId"].ToString();

            BAL.Customers.Update_Hire_Equipments(HireId, Company_Name, Authority_Name, _Address, Title_Of_Production,
                Language_Of_Production, mot, Equipment_Location, Period_Of_Production, Start_Date, End_Date, Total_Days,
                delHr, delMin, delAmPm, recHr, recMin, recAmPm, _Hire_Equipment, Payment_Mode, _Cash, Cheque_No,
                Amount_Cheque, _Balance);

            populateHireEqupm();
        }

        protected void lnkAppr_Click(object sender, EventArgs e)
        {
            LinkButton lnkApp = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkApp.Parent.Parent;

            int Cid = Convert.ToInt32(gdHireEquipment.DataKeys[row.RowIndex]["HireId"]);
            string[] Details = BAL.Customers.GetHireEquipmentById(Cid);

            string key = Details[24].ToString();

            if (lnkApp.Text.Equals("Disapprove") && ( key.Equals("0") || key.Equals("") ) )
            {
                lnkApp.Text = "Approve";
                BAL.Customers.Hire_Equip_Appr(Cid, 0);
               
            }
            else if (lnkApp.Text.Equals("Approve") && key.Equals("0"))
            {
                 lnkApp.Text = "Disapprove";
                 BAL.Customers.Hire_Equip_Appr(Cid, 1);
            }
            populateHireEqupm();
        }
    }
}